package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;



    @Override
    public PageResult<User> getUsers(UserDto userDto) {
        // 开始分页
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());

        // 执行查询
        List<User> users = userMapper.getUsers(userDto);

        // 获取分页信息
        Page<User> pageInfo = (Page<User>) users;

        // 封装成 PageResult 对象
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
    }


    @Override
    public void addUsers(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setPassword(user.getUsername()+"123");
        userMapper.insertUsers(user);
    }
}
